#include "dialogAnalysisSettings.h"

DialogAnalysisSettings::DialogAnalysisSettings(QWidget *parent)
{

    this->setWindowTitle("分析配置");
    this->setWindowFlags(windowFlags() ^ Qt::WindowContextHelpButtonHint);
    // .....
#ifdef Q_OS_ANDROID
    this->setParent(parent);
    this->setWindowFlags(Qt:: Dialog | Qt:: FramelessWindowHint);
    this->setStyleSheet("QDialog{background-color: white; padding:20px; border: none; border: 10px solid rgb(200,219,243);}");
#else
    this->setStyleSheet("QDialog{background-color: white;}");
#endif
    this->setFixedHeight(500);this->setFixedWidth(400);

    _combox_func = new QComboBox(this);
    _combox_filter = new QComboBox(this);
    _combox_ct = new QComboBox(this);
    _combox_thr = new QComboBox(this);

    _layout = new QVBoxLayout(this);
    _layout_func = new QHBoxLayout(this);
    _layout_isFilter = new QHBoxLayout(this);
    _layout_filter = new QHBoxLayout(this);
    _layout_thr = new QHBoxLayout(this);
    _layout_ct = new QHBoxLayout(this);
    _layout_baseline = new QHBoxLayout(this);
    _layout_range = new QHBoxLayout(this);
    _layout_button = new QHBoxLayout(this);

    _button_baselineAuto = new QRadioButton("自动设置", this);
    _button_baselineManual = new QRadioButton("手动设置", this);
    _buttonGroup_baseline.addButton(_button_baselineAuto);
    _buttonGroup_baseline.addButton(_button_baselineManual);

    _button_filterOn = new QRadioButton("打开", this);
    _button_filterOff = new QRadioButton("关闭", this);
    _buttonGroup_filter.addButton(_button_filterOn);
    _buttonGroup_filter.addButton(_button_filterOff);

    _lineedit_start = new QLineEdit(this);
    _lineedit_end = new QLineEdit(this);
    _combox_channel = new QComboBox(this);

    _button_no = new QPushButton("取消",this);
    _button_yes = new QPushButton("确定",this);

    // 样式调整
    _layout->setMargin(30);
    _layout->setSpacing(15);
    _combox_func->setFixedHeight(40);
    _combox_filter->setFixedHeight(40);
    _combox_ct->setFixedHeight(40);
    _combox_thr->setFixedHeight(40);
    _button_no->setFixedSize(QSize(150, 40));
    _button_no->setStyleSheet("background: #FFFFFF;\nborder-radius: 8px;\nborder: 1px solid #999999;");
    _button_yes->setFixedSize(QSize(150, 40));
    _button_yes->setStyleSheet("background-color: qlineargradient(x1: 0, y0: 0, x2: 0, y2: 1, stop: 0 #8C97ED, stop: 1 #0018D1);\nborder-radius: 8px; \ncolor: white;");
    _lineedit_end->setFixedHeight(40);
    _lineedit_start->setFixedHeight(40);
    _combox_channel->setFixedSize(QSize(100, 40));


    // 布局
    this->setLayout(_layout);
    _layout->addLayout(_layout_isFilter);
    _layout->addLayout(_layout_filter);
    _layout->addLayout(_layout_func);
    _layout->addLayout(_layout_thr);
    _layout->addLayout(_layout_ct);
    _layout->addLayout(_layout_baseline);
    _layout->addLayout(_layout_range);
    _layout->addLayout(_layout_button);


    _layout_isFilter->addWidget(new QLabel("预处理", this));
    _layout_isFilter->addWidget(_button_filterOn);
    _layout_isFilter->addWidget(_button_filterOff);
    _layout_filter->addWidget(new QLabel("预处理算法", this));
    _layout_filter->addWidget(_combox_filter);

    _layout_func->addWidget(new QLabel("函数模型", this));
    _layout_func->addWidget(_combox_func);

    _layout_thr->addWidget(new QLabel("阈值线算法", this));
    _layout_thr->addWidget(_combox_thr);

    _layout_ct->addWidget(new QLabel("相对阈值方案", this));
    _layout_ct->addWidget(_combox_ct);

    _layout_button->addWidget(_button_no);
    _layout_button->addWidget(_button_yes);

    _layout_baseline->addWidget(new QLabel("基线起止循环   ", this));
    _layout_baseline->addWidget(_button_baselineAuto);
    _layout_baseline->addWidget(_button_baselineManual);
    _layout_range->addWidget(_combox_channel);
    _layout_range->addWidget(_lineedit_start);
    _layout_range->addWidget(new QLabel("-", this));
    _layout_range->addWidget(_lineedit_end);
    _buttonGroup_baseline.setExclusive(true);
    _buttonGroup_filter.setExclusive(true);
    _button_baselineAuto->setChecked(true);
    _button_filterOn->setChecked(true);


    connect(_button_yes, SIGNAL(clicked()), this, SLOT(on_yesClicked()));
    connect(_button_no, SIGNAL(clicked()), this, SLOT(on_noClicked()));

    _combox_thr->addItem("相对阈值");
    _combox_thr->addItem("绝对阈值");

    _combox_ct->addItem("CqMAN");
    _combox_ct->addItem("Cy0");

    _combox_filter->addItem("三点均值");
    _combox_filter->addItem("五点二阶");

    _combox_func->addItem("Sigmoid");
    _combox_func->addItem("Richards");

    _combox_channel->addItem("FAM");
    _combox_channel->addItem("VIC");
    _combox_channel->addItem("ROX");
    _combox_channel->addItem("CY5");

    connect(_combox_ct, SIGNAL(activated(int)), this, SLOT(on_checkChanged()));
    connect(_combox_filter, SIGNAL(activated(int)), this, SLOT(on_checkChanged()));
    connect(_combox_thr, SIGNAL(activated(int)), this, SLOT(on_checkChanged()));
    connect(_combox_func, SIGNAL(activated(int)), this, SLOT(on_checkChanged()));
    connect(_lineedit_end, SIGNAL(editingFinished()), this, SLOT(on_checkChanged()));
    connect(_lineedit_start, SIGNAL(editingFinished()), this, SLOT(on_checkChanged()));
    connect(_button_baselineAuto, SIGNAL(clicked()), this, SLOT(on_checkChanged()));
    connect(_button_baselineManual, SIGNAL(clicked()), this, SLOT(on_checkChanged()));
    connect(_button_filterOn, SIGNAL(clicked()), this, SLOT(on_checkChanged()));
    connect(_button_filterOff, SIGNAL(clicked()), this, SLOT(on_checkChanged()));

    connect(_combox_channel, SIGNAL(activated(int)), this, SLOT(on_channelChanged()));

}

void DialogAnalysisSettings::init(PCRState *state){
    pState = state;
    _combox_channel->setCurrentIndex(0);
    QString channel = _combox_channel->currentText();
    _lineedit_end->setText(QString::number(pState->modelRange_end[channel]));
    _lineedit_start->setText(QString::number(pState->modelRange_start[channel]));
    on_checkChanged();

}

void DialogAnalysisSettings::on_noClicked(){
    this->close();
}
void DialogAnalysisSettings::on_yesClicked(){

}

void DialogAnalysisSettings::showEvent(QShowEvent *e){

    on_checkChanged();

    QDialog::showEvent(e);
#ifdef Q_OS_ANDROID
    if(this->parent() != nullptr)
        this->setGeometry(((QWidget*)parent())->width()/2-200, ((QWidget*)parent())->height()/2-this->height()/2, 400, 500);
#endif

}

void DialogAnalysisSettings::on_checkChanged(){

    if(!pState){
        return;
    }

    if(_combox_thr->currentText()=="绝对阈值"){
        pState->modelCt = -1;
    }else{
        if (_combox_ct->currentText()=="CqMAN"){
            pState->modelCt = 0;
        }else{
            pState->modelCt = 2;
        }
    }

    if(_button_filterOff->isChecked()){
        pState->modelFilter = -1;
        _combox_filter->setEnabled(false);
    }else{
        _combox_filter->setEnabled(true);
        if(_combox_filter->currentText() == "三点均值"){
            pState->modelFilter = 0;
        }else if(_combox_filter->currentText() == "五点二阶"){
            pState->modelFilter = 1;
        }
    }


    if(_combox_func->currentText() == "Sigmoid"){
        pState->modelFunc = 0;
    }else{
        pState->modelFunc = 1;
    }

    if(_combox_thr->currentText() == "绝对阈值"){
        _combox_ct->setEnabled(false);
        _button_baselineAuto->setEnabled(true);
        _button_baselineManual->setEnabled(true);
        if(_button_baselineAuto->isChecked()){
            pState->modelAutoThr = true;
            _combox_channel->setEnabled(false);
            _lineedit_end->setEnabled(false);
            _lineedit_start->setEnabled(false);
        }else{
            pState->modelAutoThr = false;
            _combox_channel->setEnabled(true);
            _lineedit_end->setEnabled(true);
            _lineedit_start->setEnabled(true);
        }
    }if(_combox_thr->currentText() == "相对阈值"){
        _combox_ct->setEnabled(true);
        _combox_channel->setEnabled(false);
        _button_baselineAuto->setEnabled(false);
        _button_baselineManual->setEnabled(false);
        _lineedit_end->setEnabled(false);
        _lineedit_start->setEnabled(false);
    }


    QString curChannel = _combox_channel->currentText();

    pState->modelRange_start[curChannel] = _lineedit_start->text().toInt();
    pState->modelRange_end[curChannel] = _lineedit_end->text().toInt();

}

void DialogAnalysisSettings::on_channelChanged(){
    QString channel = _combox_channel->currentText();
    _lineedit_end->setText(QString::number(pState->modelRange_end[channel]));
    _lineedit_start->setText(QString::number(pState->modelRange_start[channel]));
}



